package com.mdinic.blogovi.front.page.register;

import org.apache.wicket.markup.html.form.EmailTextField;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.UrlValidator;

import com.mdinic.blogovi.front.common.AbstractFormFeedbackPanel;
import com.mdinic.blogovi.model.Person;
import com.mdinic.blogovi.service.PersonService;
import com.mdinic.blogovi.service.model.RegisterResult;

public class RegisterPanel extends AbstractFormFeedbackPanel<Person> {

    private static final long serialVersionUID = 1L;

    @SpringBean
    private PersonService personService;

    public RegisterPanel() {
        super(ID, new Person());

        getForm().add(new TextField<String>(Person.FIRSTNAME).setRequired(true));
        getForm().add(new TextField<String>(Person.LASTNAME).setRequired(true));
        getForm().add(new EmailTextField(Person.EMAIL).setRequired(true));

        getForm().add(new PasswordTextField(Person.PASSWORD).setRequired(true));
        getForm().add(new PasswordTextField(Person.CONFIRM_PASSWORD).setRequired(true));

        getForm().add(new TextField<String>(Person.NICKNAME).setRequired(true));
        getForm().add(new TextField<String>(Person.WEBSITE).add(new UrlValidator()));
        getForm().add(new TextField<String>(Person.BLOG).add(new UrlValidator()));

    }

    @Override
    protected void submit(Person user) {
        RegisterResult register = personService.register(user);
        switch (register) {
        case OK:
            setResponsePage(RegisterMailSentPage.class);
            break;

        case NICK_USED:
            error("Nadimak address je zauzet.");
            break;

        case EMAIL_USED:
            error("E-mail adresa je zauzeta.");
            break;

        case PASSWORD_WEAK:
            error("Lozinka mora sadrzati 1 cifru, 1 malo slovo, 1 veliko slovo ukupne duzine od 6 do 20.");
            break;
        default:
            throw new IllegalArgumentException("State not supported " + register);
        }
    }
}
